<?php
/*
 * Created on 2016-12-29
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 require_once(dirname(__FILE__)."/PhpServerSdk/TimRestApi.php");
 
 
 function im_get_api_app()
 {
 	#读取app配置文件
	$filename = dirname(__FILE__)."/PhpServerSdk/TimRestApiConfig.json";
	$json_config = file_get_contents($filename);
	$app_config = json_decode($json_config, true);
	$sdkappid = $app_config["sdkappid"];
	$identifier = $app_config["identifier"];
	$private_pem_path = dirname(__FILE__)."/PhpServerSdk/".$app_config["private_pem_path"];
	$user_sig = $app_config["user_sig"];

	$api = createRestAPI();
	$api->init($sdkappid, $identifier);
	
	if($private_pem_path != "")
	{
		//独立模式
		if(!file_exists($private_pem_path))
		{
			return array(null , "私钥文件不存在, 请确保TimRestApiConfig.json配置字段private_pem_path正确\n $private_pem_path \n" ) ;
		}

		/**
		 * 获取usersig
		 * 36000为usersig的保活期
		 * signature为获取私钥脚本，详情请见 账号登录集成 http://avc.qcloud.com/wiki2.0/im/
		 */
		if(is_64bit()){
			if(PATH_SEPARATOR==':'){
				$signature = dirname(__FILE__)."/PhpServerSdk/signature/linux-signature64";
			}else{
				$signature = dirname(__FILE__)."\\PhpServerSdk\\signature\\windows-signature64.exe";
			}
			
		}else{
			if(PATH_SEPARATOR==':')
			{
				$signature = dirname(__FILE__)."/PhpServerSdk/signature/linux-signature32";
			}else{
				$signature = dirname(__FILE__)."\\PhpServerSdk\\signature\\windows-signature32.exe";
			}
		}
		$ret = $api->generate_user_sig($identifier, '36000', $private_pem_path, $signature);
		if($ret == null || strstr($ret[0], "failed")){
			return array(null , "获取usrsig失败, 请确保TimRestApiConfig.json配置信息正确\n $identifier , $private_pem_path , $signature " ) ;
		}
	}else if($user_sig != ""){
		//托管模式
		$ret = $api->set_user_sig($user_sig);
		if($ret == false){
			return array(null ,  "设置usrsig失败, 请确保TimRestApiConfig.json配置信息正确\n" ) ;
		}
	}else{
		return array(null , "请填写TimRestApiConfig.json中private_pem_path(独立模式)或者user_sig(托管模式)字段\n" ) ;
	}
	
	return array($api , "init ok") ;
 }
 function im_check_user_account($session)
 {
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0)
	{
		return array($GLOBALS["ERR"]["ERR_USER_NOTFIND"] , $GLOBALS["MES"]["ERR_USER_NOTFIND"] ) ;
	}
	$userinfo = array() ;
	util_set_USER($userinfo , $uid) ;
	if (count($userinfo['username']) == 0){
		return array($GLOBALS["ERR"]["ERR_USER_NOTFIND"] , $GLOBALS["MES"]["ERR_USER_NOTFIND"] ) ;
	}
	if (count($userinfo['username']) == 0){
		$userinfo['nickname'] = $userinfo['username'] ;
	}
	
	$identifier = $userinfo['username'] ; 
	$nick = $userinfo['nickname'] ; 
	$face_url = $userinfo['avatar']['url'] ;
	
	return array($GLOBALS["ERR"]["OK"] , $identifier , $nick , $face_url) ;
 }
 
 
 // 独立模式帐号同步
 function im_call_account_import($json , $session)
 {
 	$ret = array() ;
	$ret['data'] = array() ;
	
	$userinfo = im_check_user_account($session) ;
	if (count($userinfo) < 2){	$ret['status'] = 999 ;			$ret['message'] = "调用错误,请检查接口" ;	return $ret ;	}
	if ($userinfo[0] != 0){		$ret['status'] = $userinfo[0] ;	$ret['message'] = $userinfo[1] ;	return $ret ;	}
	
	$cls_arr = im_get_api_app() ;
	if (count($cls_arr) != 2){	$ret['status'] = 998 ;	$ret['message'] = "调用错误,请检查接口" ;	return $ret ;	}
	if ($cls_arr[0] == null){	$ret['status'] = 997 ;	$ret['message'] = $cls_arr[1] ;		return $ret ;	}	
	
	
	$rnt = $cls_arr[0]->account_import($userinfo[1], $userinfo[2], $userinfo[3]);	
	
	$ok = intval($rnt['ErrorCode']) ;
	$st = $rnt['ActionStatus'] ;
	if ($ok == 0 && $st == "OK"){
		$ret['status'] = 0 ;
		$ret['message'] = "同步成功" ;
	}else{
		$ret['status'] = $ok ;
		$ret['message'] = $rnt["ErrorInfo"] ;
	}	
	return $ret ;
 }
 
?>
